package client.gui;

import java.awt.Color;

import sharedspace.*;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Rectangle;
import javax.swing.*;
import sharedspace.User;
import client.gui.GUI_AbstractMainMenu.MainMenuType;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import messages.MessageAllExams;
import messages.MessageAllExecuteExams;
import messages.MessageApprovedTestByExecuteTest;
import messages.MessageApprovedTestByExecuteTestReply;
import messages.MessageCourse;
import messages.MessageExecutedExamListByTeacherID;
import messages.MessageExecutedExamListReply;
import messages.MessageStudentListByExam;
import messages.MessageStudentListByExamReply;
import messages.MessageSubject;
import messages.MessageSubjectReply;
import messages.MessageTeacherSubject;

import java.io.IOException;
import java.util.ArrayList;
import java.awt.BorderLayout;
import java.awt.Container;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.util.List;
/**
 * This JPanel is a panel for Teacher to approve students executive test grades
 * @author Amir S.
 * @author Jacob C.
 * @author Maya G.
 * @author Michael V.
 *
 */
public class GUI_Teacher_ViewGrades extends GUI_AbstractMainMenu{
	private JScrollPane gradesScroller = null;
	private DefaultListModel gradesList = null;
	private JList listGrades = null;
	private JTable gradeJTable = null;
	private List<ApprovingGrade> grList = null;
	public JButton btnBack = null;
	private ArrayList<Course> courses;
	private List<ExecuteTest> ext;
	public JComboBox<String> comboBoxExecuite;
	private MessageSubjectReply msgsrep;
	private String executeTest;
	
	Client_GUI ms=null;
	


	public GUI_Teacher_ViewGrades(Client_GUI MS) {
		super(MainMenuType.MANAGER,MS);
		this.ms = MS;
		this.initiateManager();
		greetMsg.setText("Choose Exam:");
		mainMenu.setText("View Grades");

	}
	
	public void initiateManager()
	{
		this.setSize(1000, 700);	
		this.setLayout(null);
		background.add(getbtnBack(),null);
		//background.add(getExecuteExamsTable(),null);
		background.add(getGradesTable(),null);
		background.add(btnBack);
		
		comboBoxExecuite= new JComboBox<String>();
		comboBoxExecuite.setSelectedIndex(-1);
		comboBoxExecuite.setBounds(150, 147, 450, 28);
		background.add(comboBoxExecuite);
		
		JLabel lblNewLabel = new JLabel("Grades:");
		lblNewLabel.setFont(new Font("Arial", Font.BOLD, 24));
		lblNewLabel.setForeground(Color.BLUE);
		lblNewLabel.setBounds(53, 201, 208, 30);
		background.add(lblNewLabel);
		
		getExecuteTestID();
		
	}

	private JButton getbtnBack()
	{
		if (btnBack == null)
		{
			btnBack = new JButton("Back");	
			btnBack.setBounds(53, 28, 107, 30);
			btnBack.setVisible(true);
		}
		
		return btnBack;
	}

	
	private void getExecuteTestID(){
		int i;
		MessageExecutedExamListByTeacherID tec = new MessageExecutedExamListByTeacherID(ms.lg.getUser().getUserID());
		try {
			ms.client.sendToServer(tec);
			MessageExecutedExamListReply MsgRep =(MessageExecutedExamListReply) ms.client.GetMessage();
			ext = MsgRep.getExTestList();
			comboBoxExecuite.removeAllItems();
			for(i=0;i<ext.size();i++)
			{
				comboBoxExecuite.addItem("Test ID:"+ext.get(i).getExecuteTestID() + " Subject:" + ext.get(i).getTest().getSubject().getSubjectName() + " Course:" + ext.get(i).getTest().getCourse().getCourseName() + " Year:" + ext.get(i).getTestYear() + " Semester:" + ext.get(i).getTestSemster());
			}
			} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		comboBoxExecuite.setSelectedIndex(-1);
		
		comboBoxExecuite.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e4) {
			int i;
			if(comboBoxExecuite.getSelectedIndex()>-1){   //if a subject was selected 

					executeTest = ext.get(comboBoxExecuite.getSelectedIndex()).getExecuteTestID();
					System.out.println(executeTest.toString());
						
					MessageApprovedTestByExecuteTest msgATE=new MessageApprovedTestByExecuteTest(executeTest);
						
					try {
								//Sending the login message to server
								
								ms.client.sendToServer(msgATE);	
				
								} catch (IOException e1) {
								// TODO Auto-generated catch block
								e1.printStackTrace();
							} 

						//Getting reply to login from client
						MessageApprovedTestByExecuteTestReply msgATErep = (MessageApprovedTestByExecuteTestReply) ms.client.GetMessage();	
						grList = msgATErep.getAlist();
						gradesList.clear();
						for(i=0;i<msgATErep.getAlist().size();i++)
						{
							gradesList.addElement("Student ID: " + msgATErep.getAlist().get(i).getStudentInTest().getStudent().getUserID() + "   ,Name: " + msgATErep.getAlist().get(i).getStudentInTest().getStudent().getUserFirstName() +  msgATErep.getAlist().get(i).getStudentInTest().getStudent().getUserLastName() + "   ,Exam Grade: " + msgATErep.getAlist().get(i).getStudentFianlGrade());
						}
					}
				}
			//end if
		
});
	
}// end getExecuteTest
	
	private JScrollPane getGradesTable()
	{
		int i;
		if (gradeJTable == null)
		{
			gradesList = new DefaultListModel();
			listGrades = new JList(gradesList);
			listGrades.setVisibleRowCount(3);
			listGrades.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
			listGrades.setSelectedIndex(0);
			gradesScroller = new JScrollPane(listGrades);
			gradesScroller.setBounds(53, 250, 774, 225);
		} 
		return gradesScroller;
	}
}